(function(){
	
		var NUM_MAKES = 56;		//Determines the number of circles/makes on the page
		var RAD_TO_PAGE_SCALE = 30; //Controls the ratios of the circles to the page (size of dots)
		var TURN_RADIUS = Math.PI / 7; //Controls how much the angle of the next dot changes
		var POINT_TO_PAGE_SCALE = 2.5; //Controls the ratio of the distance of points from the center to the page size
		var SVG_SIZE = .97; //Ratio of the svg element to the total page
		
		
		var FRAME_WIDTH = window.innerWidth,
			FRAME_HEIGHT = window.innerHeight;
		
		var MIN_DIM = d3.min([FRAME_WIDTH,FRAME_HEIGHT]); //The smaller out of the window height and width
		var NUM_TO_PIXELS = MIN_DIM / 10; //The ratio of actual calculations to the number of pixels visually
		
		var POINTS= getPoints();
		
		//Scale the points up to match the position on the page in pixels
		scaleAll(POINTS, NUM_TO_PIXELS);
		
		//Create frame	
		var svg = d3.select("body").append("svg")
			.attr("width", FRAME_WIDTH * SVG_SIZE)
			.attr("height", FRAME_HEIGHT * SVG_SIZE)
			.append("g")
			.attr("transform", "translate(" + FRAME_WIDTH/2 + "," + (FRAME_HEIGHT/2) +")");

		//create main path spiral
		/*var mainSpiral = d3.svg.line.radial()
		  .interpolate("cardinal")
		  .angle(function(theta) {return theta + Math.PI/2;})
		  .radius(function(theta) {
			  return NUM_TO_PIXELS * Math.log(theta);
		  });
		
		console.log(NUM_TO_PIXELS);
		
		//Create main spiral
		svg.selectAll(".mainSpiral")
			.data([d3.range(Math.PI/3, 20*Math.PI, 0.01)])
			.enter().append("path")
			.attr("class", "mainSpiral")
			.attr("d", mainSpiral);*/

		//Create galaxy spirals
		/*for(var i=0; i<2*Math.PI; i+=(Math.PI/4)){
			var spiral = d3.svg.line.radial()
				.interpolate("cardinal")
				.angle(function(theta) {return theta;})
				.radius(function(theta) {
					return MIN_DIM/10 * (theta - i);
				});
			svg.selectAll("spiral")
				.data([d3.range(i, i + 4*Math.PI/3, 0.01)])
				.enter().append("path")
				.attr("class", "spiral")
				.attr("d", spiral);
		}*/
	
		//Create circles
		svg.append("g")
			.selectAll(".make")
			.data(POINTS)
			.enter()
			.append("circle")
			.attr("cx", function(d){ return d[0]; })
			.attr("cy", function(d){ return d[1]; })
			.attr("r", function(d,i){ return getRadii(i);})
			.attr("class", "make");
		
		//Size = MAX /(sqrt(i)+1) + MIN
		function getRadii(i){
			var MAX = MIN_DIM / RAD_TO_PAGE_SCALE;
			var MIN = MAX / 4;
			var interval = (MAX-MIN)/NUM_MAKES;
			return MAX - i*interval;
		}
		
		//scales the arrays of points by the given factor
		function scaleAll(array, scaleFactor){
			console.log(scaleFactor);
			console.log(array);
			for(var i=0; i<array.length; i++){
				array[i][0] *= scaleFactor;
				array[i][1] *= scaleFactor;
			}
		}
		
		//finds the minimum length of array elements in an array of arrays
		function findMinLen(allArrays){
			var minLen = Number.MAX_VALUE; 
			for(var i=0; i<allArrays.length; i++){
				var len = allArrays[i].length;
				if(len < minLen){
					minLen = len;
				}
			}
			return minLen;
		}
		
		//Returns an array of two element arrays representing coordinates at which to place circles
		function getPoints(){
			var maxDist = getDistToPoint(NUM_MAKES-1);
			var windowSpace = MIN_DIM / 2;
			while(windowSpace < maxDist){
				POINT_TO_PAGE_SCALE++;
				maxDist = getDistToPoint(NUM_MAKES-1);
			}
			var allPoints = [];
			for(var i=Math.PI / 3; i<NUM_MAKES; i++){
				//Multiplying by 5 balances out the effect of squaring 
				var theta = 5 * Math.sqrt(i*TURN_RADIUS); 
				//Square to get a more gradual decrease in radius change
				var r = Math.pow(Math.log(theta), 2) / POINT_TO_PAGE_SCALE;
				allPoints.push(polarToCart([r, theta]));
			}
			return allPoints;
		}
		
		//Returns the distance from the center of the page to the [pointNumber]th point on the page
		function getDistToPoint(pointNumber){
			return (Math.pow(Math.log(5 * Math.sqrt(pointNumber*TURN_RADIUS)), 2) 
					/ POINT_TO_PAGE_SCALE * NUM_TO_PIXELS + (MIN_DIM / RAD_TO_PAGE_SCALE));
		}
		
		//takes a two element array [r, theta] and returns a two-element array [x, y]
		function polarToCart(polarArray){
			var cartesian = [];
			var r = polarArray[0];
			var theta = polarArray[1];
			cartesian.push(r * Math.cos(theta));
			cartesian.push(r * Math.sin(theta));
			return cartesian;
		}
	}
)();